cmake_minimum_required(VERSION 3.16)
project(Mesen2 LANGUAGES C CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)

if (QT_VERSION_MAJOR EQUAL 6)
    find_package(Qt6 COMPONENTS Widgets OpenGLWidgets Sql REQUIRED)
else()
    find_package(Qt5 COMPONENTS Widgets Sql REQUIRED)
endif()

file(GLOB_RECURSE CORE_SRC        CONFIGURE_DEPENDS "Core/*.cpp")
file(GLOB_RECURSE UTIL_SRC        CONFIGURE_DEPENDS "Utilities/*.[ch]pp" "Utilities/*.c")
file(GLOB_RECURSE SEVENZIP_SRC    CONFIGURE_DEPENDS "SevenZip/*.c")
file(GLOB_RECURSE LUA_SRC         CONFIGURE_DEPENDS "Lua/*.c")

if (WIN32)
    set(WIN32_EXECUTABLE ON)
    add_definitions(-DUNICODE -D_UNICODE)
    list(FILTER LUA_SRC EXCLUDE REGEX ".*(unix|serial).*\\.c$")
endif()

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    Core
    Utilities
    SevenZip
)

if (WIN32)
    include_directories(Windows)
elseif(UNIX)
    include_directories(Linux)
endif()

add_subdirectory(Qt)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks/pre-commit)
        file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/pre-commit
            DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks
            FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
        )
    endif()
endif()